package com.util;

import java.io.Serializable;


import com.alibaba.fastjson.JSONObject;

/**
 * 服务器和客户端,服务器和服务器直接数据传输的对象
 **/
public class PBMessage implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private short code; // 协议号
	private int id; // 玩家ID
	private int type; // 玩家ID
	private JSONObject message; // Proto

	private PBMessage() {
	}

	public PBMessage(short code) {
		this(code, -1);
	}

	public PBMessage(short code, int id) {
		this.code = code;
		this.id = id;
	}

	
	public short getCode() {
		return code;
	}

	public void setCode(short code) {
		this.code = code;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public JSONObject getMessage() {
		return message;
	}

	public void setMessage(JSONObject message) {

		this.message = message;
	}
	
	public void buildPBMessage(JSONObject message) {
		if(message.containsKey("userId")){
			this.id = message.getInteger("userId");
		}
		if(message.containsKey("code")){
			this.code = message.getShort("code");
		}
		if(message.containsKey("type")){
			this.type = message.getShort("type");
		}
		this.message = message;
	}
	
	/**
	 * 创建空消息(避免外部实例化)
	 * 
	 * @return
	 */
	public static PBMessage buildPBMessage() {
		return new PBMessage();
	}



	


}
